
/****************************************************************/
/*     export.cpp      Functions for exporting art/palettes     */
/*                     By: Josh Fiorio                          */
/****************************************************************/

#include "export.h"

extern long pcolor;    /* TODO: pass as an arg to the functions */
extern int artMag;     /* TODO: same as up above =) */

void BufToBitmap(char fn[], FrameBuffer &it)
{
	//Determine the portion of the image to save, the portion with pixels
    int xmost=0;
    int ymost=0;
	int xlp=0;
	int ylp=0;

	//get new w&h
    for (ylp=0; ylp < it.height; ylp++)
	{
        for (xlp=0; xlp < it.width; xlp++)
		{
			if (it.GetPixel(xlp, ylp) != pcolor && it.GetPixel(xlp,ylp) != -1)
			{
                if (xlp > xmost) { xmost = xlp; }
				if (ylp > ymost) { ymost = ylp; }
            }	//end if
		}	//end for
	}	//end for
	xmost+=1; ymost+=1;		//units of width and height

	//Create image
	FILE *bf;
	bf=fopen(fn, "w+b");
	
	//pos in file
	long bpos=0;
	long wv=0;

	//go to beginning of file
	rewind(bf);
	//fseek(bf, bpos, 0);
	
	//--FILE HEADER--//
	//write header
	fwrite("BM", 2, 1, bf);
		bpos=bpos+2;
		fseek(bf, bpos, 0);
	
	//For now, we will write zero
	wv=0; fwrite(&wv, 4, 1, bf);	
		bpos=bpos+4;
		fseek(bf, bpos, 0);

	//Reserved 1, always set to zero
	wv=0; fwrite(&wv, 2, 1, bf);	
		bpos=bpos+2;
		fseek(bf, bpos, 0);

	//Reserved 2, always set to zero
	wv=0; fwrite(&wv, 2, 1, bf);
		bpos=bpos+2;
		fseek(bf, bpos, 0);

	//Write address of start of pixels
	wv=0x36; fwrite(&wv, 4, 1, bf);
		bpos=bpos+4;
		fseek(bf, bpos, 0);

	//--INFO HEADER--//
	//Write size of info header
	wv=40; fwrite(&wv, 4, 1, bf);
		bpos=bpos+4;
		fseek(bf, bpos, 0);

	//Write image width, width of our buf
	fwrite(&xmost, 4, 1, bf);
		bpos=bpos+4;
		fseek(bf, bpos, 0);

	//Write image height, height of our buf
	fwrite(&ymost, 4, 1, bf);
		bpos=bpos+4;
		fseek(bf, bpos, 0);

	//Write # of planes, 1 in our case
	wv=1; fwrite(&wv, 2, 1, bf);
		bpos=bpos+2;
		fseek(bf, bpos, 0);

	//Write bpp, 24 in our case
	wv=24; fwrite(&wv, 2, 1, bf);
		bpos=bpos+2;
		fseek(bf, bpos, 0);

	//Write compression, none in our case
	wv=0; fwrite(&wv, 4, 1, bf);
		bpos=bpos+4;
		fseek(bf, bpos, 0);

	//Write size of pixel data, 0 since we have no compression
	wv=0; fwrite(&wv, 4, 1, bf);
		bpos=bpos+4;
		fseek(bf, bpos, 0);

	//Pixels in x dir per meter, more bull, just ignore and write 0 =P
	wv=0; fwrite(&wv, 4, 1, bf);
		bpos=bpos+4;
		fseek(bf, bpos, 0);

	//Pixels in y dir per meter, just ignore and write 0
	wv=0; fwrite(&wv, 4, 1, bf);
		bpos=bpos+4;
		fseek(bf, bpos, 0);
	
	//Number of colors used in our bitmap, we can use as many, so 0
	wv=0; fwrite(&wv, 4, 1, bf);
		bpos=bpos+4;
		fseek(bf, bpos, 0);

	//Important colors, all of our colors are important, so 0 again
	wv=0; fwrite(&wv, 4, 1, bf);
		bpos=bpos+4;
		fseek(bf, bpos, 0);

	//--PIXEL DATA--//
	//declare vars.
	long pcol, flen=0;
	int pad, x, zero=0;
	BYTE r,g,b=0;

	//determine how much to pad after each scanline
	pad=(4-((xmost*3)%4));
		if (pad>=4) {pad=0;}

	//write pixels
	for (int y=0; y<ymost; y++)
	{
		for (x=0; x<xmost; x++)
		{
			//get pix r, g, and b values
			pcol=it.GetPixel(x,ymost-y-1);
			r=GetRValue(pcol); g=GetGValue(pcol); b=GetBValue(pcol);

			//write for this pix
			fwrite(&b, 1, 1, bf);
				bpos=bpos+1;
				fseek(bf, bpos, 0);
			fwrite(&g, 1, 1, bf);
				bpos=bpos+1;
				fseek(bf, bpos, 0);
			fwrite(&r, 1, 1, bf);
				bpos=bpos+1;
				fseek(bf, bpos, 0);
		}	//end for

		fwrite(&zero, pad, 1, bf);
			bpos=bpos+pad;
			fseek(bf, bpos, 0);
	}	//end for

	//get file size
	flen=bpos;
	
	//go to file size that was previously set to zero in the file
	bpos=2;
	fseek(bf, bpos, 0);

	//change it new our new file size
	fwrite(&flen, 4, 1, bf);
		bpos=bpos+4;
		fseek(bf, bpos, 0);

	fclose(bf);		//done, don't need any more
}	//end of func.

void BufToUArt(char fn[], FrameBuffer &it, palette &pal)
{
	//Create art file
	FILE *af;
	af=fopen(fn, "w+b");
    
	//Declare varibles
	int xlp = 0;
	int ylp = 0;
	int xlp2 = 0; 
	int ylp2 = 0;
	int xmost = 0;
	int ymost = 0;
	int xpx	= 0;
	int xpy = 0;
	long pr1 = 0;
	long pr2 = 0;
	long apos = 0;
	BYTE p1 = 0;
	BYTE p2 = 0;
	BYTE thebyte = 0;
    
	//get new w&h
    for (ylp=0; ylp < it.height; ylp++)
	{
        for (xlp=0; xlp < it.width; xlp++)
		{
			if (it.GetPixel(xlp, ylp) != pcolor && it.GetPixel(xlp,ylp) != -1)
			{
                if (xlp > xmost) { xmost = xlp; }
				if (ylp > ymost) { ymost = ylp; }
            }	//end if
		}	//end for
	}	//end for
	xmost+=1; ymost+=1;										//units of width and height
	xpx=(8-(xmost%8)); xpy=(8-(ymost%8));					//find numb of extra pixels
	xmost=xmost+xpx; ymost=ymost+xpy;						//units of 8, tiles

    for (xlp2=0; xlp2 <= (xmost/8)-1; xlp2++)
	{
		for (ylp2=0; ylp2 <= (ymost/8)-1; ylp2++)
		{
			for (ylp=0; ylp <= 7; ylp++)
			{
				for (xlp=0; xlp <= 7; xlp=xlp+2)
				{
					//Get both pixels colors
					pr1 = it.GetPixel(((xlp) + ((xlp2) * 8))-(xpx), (ylp + ((ylp2) * 8))-(xpy));
					pr2 = it.GetPixel(((xlp+1) + ((xlp2) * 8))-(xpx), (ylp + ((ylp2) * 8))-(xpy));
					if (pr1 == -1){ pr1 = pcolor; } 
					if (pr2 == -1){ pr2 = pcolor; }

					//Get number of color in palette color corresponds to
					for (short chk=0; chk<=15; chk++)
					{
						if (c256to32(pr1) == pal.colors[chk])
							p1=chk;
						//end if
						if (c256to32(pr2) == pal.colors[chk])
							p2=chk;
						//end if
					}	//end for

					//combine both pixels into a byte and write
					thebyte = (p1 * 16) + p2;
					fwrite(&thebyte, 1, 1, af);
						apos=apos+1;
						fseek(af, apos, 0);
				}	//end for
			}	//end for
		}	//end for
	}	//end for
        
	//close file
	fclose(af);
		
	//Get palette file name by taking off the ext. and placing
	// a new .tpl on the end and then save the new palette
	char *palname=(char*)malloc(MAX_PATH);
	*(strrchr(fn, '.'))='\0';
	strcpy(palname, fn);
	strcat(palname, ".tpl\0");
	PaletteToFile(palname, pal);
}	//end func.

void PaletteToFile(char fn[], palette &it)
{
	//Create TLP Palette file
	FILE *pf;
	pf=fopen(fn, "w+b");
	long ppos = 0;
	
	//Declare vars.
	BYTE bbyte = 0;
    long r = 0;
	long g = 0;
	long b = 0;
    
    //Write header
	fwrite("TPL", 3, 1, pf);
		ppos = ppos + 3;
		fseek(pf, ppos, 0);
	fwrite(&bbyte, 1, 1, pf);
		ppos = ppos + 1;
		fseek(pf, ppos, 0);

    //write colors
    for (int lp=0; lp <= 15; lp++)
	{
        //get rgb values
        r = GetRValue(c32to256(it.colors[lp]));
        g = GetGValue(c32to256(it.colors[lp]));
        b = GetBValue(c32to256(it.colors[lp]));

		//write rgb values
		fwrite(&r, 1, 1, pf);
			ppos = ppos + 1;
			fseek(pf, ppos, 0);
		fwrite(&g, 1, 1, pf);
			ppos = ppos + 1;
			fseek(pf, ppos, 0);
		fwrite(&b, 1, 1, pf);
			ppos = ppos + 1;
			fseek(pf, ppos, 0);
	}	//end for
   
	//close file
    fclose(pf);
}	//end of func.
